package m1;

import com.rabbitmq.client.*;

import java.io.IOException;
import java.util.concurrent.TimeoutException;

public class Consumer {//消费者类

    public static void main(String[] args) throws IOException, TimeoutException {
        ConnectionFactory f = new ConnectionFactory();
        f.setHost("192.168.64.140");
        f.setPort(5672);
        f.setUsername("admin");
        f.setPassword("admin");
        Connection con = f.newConnection();
        Channel c = con.createChannel();
        c.queueDeclare("hello-world ",true,false,false,null);
        /*创建回调对象*/                                /*取消处理的回调对象*/
        DeliverCallback deliverCallback = new DeliverCallback() {
            public void handle(String s, Delivery delivery) throws IOException {
                byte[] a = delivery.getBody();
                String s1 = new String(a);
                System.out.println(s1);
            }
        };
        /*创建取消回调处理对象*/
        CancelCallback cancelCallback = new CancelCallback() {
            public void handle(String s) throws IOException {

            }
        };
        /*从,hello-world队列接受消息，把消息传给回调对象处理*/
        /*true：自动确认 false：手动确认
        确认方式 ACK -- AcknowLedgment*/
        String hd = c.basicConsume("hello-world", false,deliverCallback ,cancelCallback);
    }
}
